package com.chao.gulimall.product.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.chao.common.utils.PageUtils;
import com.chao.common.utils.Query;

import com.chao.gulimall.product.dao.CategoryDao;
import com.chao.gulimall.product.entity.CategoryEntity;
import com.chao.gulimall.product.service.CategoryService;


@Service("categoryService")
public class CategoryServiceImpl extends ServiceImpl<CategoryDao, CategoryEntity> implements CategoryService {

    //无法注入  categoryDao 是一个Mapper
//    @Autowired
//    CategoryDao categoryDao;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<CategoryEntity> page = this.page(
                new Query<CategoryEntity>().getPage(params),
                new QueryWrapper<CategoryEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public List<CategoryEntity> listWihTree() {
        //1、查出所有分类
        List<CategoryEntity> entities = baseMapper.selectList(null);
        //2、组装成父子树形结构
        //2.1 先找到所有的一级分类
        //完全写法  （）->{return xxx}
//        List<CategoryEntity> level1Menus = entities.stream().filter((item) -> {
//            return item.getParentCid() == 0;
//        }).collect(Collectors.toList());
        //部分省略写法  xx->{return xxx}
//        List<CategoryEntity> level1Menus = entities.stream().filter(item -> {
//            return item.getParentCid() == 0;
//        }).collect(Collectors.toList());
        //最简略写法   xx-> xxx
//        List<CategoryEntity> level1Menus = entities.stream().filter(item ->
//             item.getParentCid() == 0
//        ).collect(Collectors.toList());

        List<CategoryEntity> level1Menus = entities.stream().filter(categoryEntity ->
                categoryEntity.getParentCid() == 0
        ).map((categoryEntity) -> {
            categoryEntity.setClildren(getChildrens(categoryEntity.getCatId(),entities));
            return categoryEntity;
        }).sorted((menu1,menu2)->{
            return (menu1.getSort()==null?0:menu1.getSort())-(menu2.getSort()==null?0:menu2.getSort());
        }).collect(Collectors.toList());

        return level1Menus;
    }

    /**
     * 递归查找所有 菜单子菜单
     * @param catId
     * @param all
     * @return
     */
    private List<CategoryEntity> getChildrens(Long catId, List<CategoryEntity> all) {

        List<CategoryEntity> children = all.stream().filter(
                item -> item.getParentCid() == catId
        ).map(item->{
            //1、找到子菜单
            item.setClildren(getChildrens(item.getCatId(),all));
            return item;
        }).sorted((menu1,menu2)->{
            //2、子菜单排序
            return (menu1.getSort()==null?0:menu1.getSort())-(menu2.getSort()==null?0:menu2.getSort());
        }).collect(Collectors.toList());

        return children;
    }

}
